#include "userMain.h"

#include <project.h>

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
uint8 userLightSensorDetectLevel=1; //adc detect result
uint8 userLightSensorDetectLevelPre = 0;
uint8 userMotionDetect=0;           //adc detect result
uint8 userMotionEnable=1;
uint8 userLightSensorEnable=1;
uint8 userMotionLevel=1;
uint8 userLightDefaultBindEnable=1;
uint8 userRelayDefaultBindEnable=1;
uint8 userLightSensorLevelDefault=5;		//the reference light sensor detect Level
uint16 userLightSensorDetectValue=0;
uint16 userLightSensorDetectValueTemp=0;
uint16 userMotionSensorDetectValue=0;
uint8 userZeroDetect=0;
//uint16 userMotionSensorDetectValue=0;

#define  SQRT_ALG   0
extern uint8 userStartFlag;  //上电开机标志位，区分上电30S内时间（不会上报传感器检测数据）
extern unsigned char userTxBuf[];
extern uint8 userCloseDelay;
#define     LIGHT_SENSOR_OFFSET     100
void sqrtsf(uint16  cunnt,uint16  adcvale)
{
    
     
    #if  SQRT_ALG
	uint32 MotionValueall=0;
	uint16 pjValueall=0;
	uint16  overvale=0;
	uint16 i=0;
    static uint16 userMotionValue[305]={0};
	userMotionValue[cunnt] = adcvale;
  if(cunnt>300)
{
       for( i=0;i<300;i++) 
            {
            MotionValueall +=userMotionValue[i];  
            }
			pjValueall=(MotionValueall/300);        
			MotionValueall=0;
			  for( i=0;i<300;i++)
            {
            MotionValueall += pow((abs(userMotionValue[i]-pjValueall)),2);
           
            }
                 
           MotionValueall /= 300;           
       overvale = sqrt(MotionValueall);  
    if( ( overvale>>8)>3)  userMotionDetect = 1;
         else  userMotionDetect = 0;
    userTxBuf[0] = 0x03;
    userTxBuf[1] = 0x07;
    userTxBuf[2] = 0xaa;
    userTxBuf[3] = overvale;
	userTxBuf[4] =( overvale>>8);	
		userTxBuf[5] = 0xaa;
    userUartSendData(7);
   
}
      #endif
    
}
void userLightSensorAdcSampleFunc(void)
{
    static unsigned char user_i;
    static unsigned int userSum = 0;
    static unsigned int uDetectValueH=10;
    static unsigned int uDetectValueL=0;
 //   static unsigned char uSendNeedFlag = 0;
    if( userLightSensorEnable == 0 ) return ;
    userLightSensorDetectValue = CapSense_AdcReadResult_mVolts(CapSense_AdcCHANNEL_1);
    if(userStartFlag) return ;
    if( (userLightSensorDetectValue>uDetectValueL) &&(userLightSensorDetectValue<uDetectValueH))
    {
        user_i++;
        userSum += userLightSensorDetectValue;
    }
    else
    {
        user_i = 0;
        userSum = 0;
        uDetectValueH = userLightSensorDetectValue + LIGHT_SENSOR_OFFSET;
        uDetectValueL = userLightSensorDetectValue - LIGHT_SENSOR_OFFSET;
        if(userLightSensorDetectValue < LIGHT_SENSOR_OFFSET) uDetectValueL = 0;
     //   uSendNeedFlag = 1;
    }
    if(user_i > 15)
    {
        userSum >>= 4;
        userLightSensorDetectDealFunc(userSum);
        user_i = 0;
        userSum = 0;
    }
    _NOP;
}

void userLightSensorDetectDealFunc(unsigned int uLightSensorValue)
{   
    uint16 uLightVar;
    uLightVar = uLightSensorValue/250;
    if(uLightVar > 9) uLightVar = 9;
    userLightSensorDetectLevel = uLightVar+1;
    userBackLightLevelSetFunc(uLightSensorValue);
   
    if(uLightSensorValue < 150)   userLightSensorDetectLevel  = 1 ;
    else if(uLightSensorValue < 500)   userLightSensorDetectLevel  = 2 ;
  //  else if(uLightSensorValue < 1000)   userLightSensorDetectLevel  = 3 ;
  //  else if(uLightSensorValue < 1500)   userLightSensorDetectLevel  = 4 ;
   //  else           userLightSensorDetectLevel  = 5 ;
    
    if(userLightSensorDetectLevel == userLightSensorDetectLevelPre) return ;
    userLightSensorDetectLevelPre = userLightSensorDetectLevel;
    /*********
    userTxBuf[0] = 0x03;
    userTxBuf[1] = 0x07;
    userTxBuf[2] = userLightSensorDetectLevel;
    userTxBuf[3] = 0x08;
    userTxBuf[4] = uLightSensorValue>>8;
    userTxBuf[5] = uLightSensorValue&0x00FF;
    for(user_i=0;user_i<6;user_i++)
    {
        userTxCheckSum += userTxBuf[user_i];
    }
    userTxBuf[user_i] = userTxCheckSum;
    userUartSendData(7);*****************/
    userSendLightSensorInfoFunc();
    _NOP;
}
void userMotionSensorEnableFunc(unsigned char enableValue)
{
    if(enableValue)
    {
        userMotionEnable = 1;
        Radar_EN_Write(1);
    }
    else
    {
        userMotionEnable = 0;
        Radar_EN_Write(0);
    }
}
void userLightSensorEnableFunc(unsigned char enableValue)
{
    if(enableValue)
    {
        userLightSensorEnable = 1;
    }
    else
    {
        userLightSensorEnable = 0;
    }
}


void userSensorInit(void)
{
    userMotionSensorEnableFunc(userMotionEnable);
}

unsigned int userRelayCloseTime = 0;
uint8 userMotionDetectPre=0;
uint16 userMotionCnt = 0;
uint16 userMotionCntA = 0;
void userMotionMarkTickFunc(void)
{
    if(userMotionCnt < 15000)   
        userMotionCnt++;
    if(++userMotionCntA > 1000)
    {
        userMotionCntA = 0;
        userRelayCloseTime++;
    }
}

unsigned int uMotionValueH=1900;
unsigned int uMotionValueL=1500;
void userMotionSensitivitySet(unsigned char valueA,unsigned char valueB)
{
    unsigned int uVar;
    uVar = (uint16)(valueA & 0x0F);
    uVar = uVar << 8;
    uVar += valueB;
    if(valueA & 0x80)
    {
        uMotionValueH = uVar;
    }
    else
    {
        uMotionValueL = uVar;
    }
}

uint8 userMotionDetectFlag = 0;
uint8 userMotionDetectFlagPre = 0;
void userMotionSensorAdcSampleFunc(void)
{
    static unsigned char user_m=0;
    static unsigned char user_n = 0;
      if( userMotionEnable == 0 ) return ;
    userMotionSensorDetectValue = CapSense_AdcReadResult_mVolts(CapSense_AdcCHANNEL_0);
     sqrtsf(userMotionCnt,userMotionSensorDetectValue);
    if(userStartFlag) 
    {
        userMotionCnt = 0;
        return ;
    }
    if(userMotionLevel)
    {
     //   if((userMotionSensorDetectValue > 1900)||(userMotionSensorDetectValue < 1500))
         if((userMotionSensorDetectValue > 1900))
       // if((userMotionSensorDetectValue > uMotionValueH)||(userMotionSensorDetectValue < uMotionValueL))
        {
            user_m++;
        }
        else 
        {
            user_n++;
        }
        if(userMotionCnt > 300)
        {
            _NOP;
	
 
            if(user_m > 9)
            {
                //userMotionDetect = 1;
                userMotionDetectFlag = 1;
                userMotionCnt = 0;
                user_m = 0;
                user_n = 0;
            }
            else //if(userMotionCnt > 300)
            {
                //userMotionDetect = 0;
                userMotionDetectFlag = 0;
                userMotionCnt = 0;
                user_m = 0;
                user_n = 0;
            }
            //userMotionCnt = 0;
            //user_m = 0;
            //user_n = 0;
        }
    }
    else
    {
       // if((userMotionSensorDetectValue > 2100)||(userMotionSensorDetectValue < 1300))
         if((userMotionSensorDetectValue > 2100)) 
        {
            user_m++;
        }
        else 
        {
            user_n++;
        }
        if(userMotionCnt > 300)
        {
            _NOP;
            if(user_m > 9)
            {
                //userMotionDetect = 1;
                userMotionDetectFlag = 1;
                userMotionCnt = 0;
                user_m = 0;
                user_n = 0;
            }
            else //if(userMotionCnt > 300)
            {
                //userMotionDetect = 0;
                userMotionDetectFlag = 0;
                userMotionCnt = 0;
                user_m = 0;
                user_n = 0;
            }
            //userMotionCnt = 0;
            //user_m = 0;
            //user_n = 0;
        
        }
        
    }
    if(userMotionDetectFlag != userMotionDetectFlagPre)
    {
        userMotionDetectPre = userMotionDetect;
        //userSendMotionInfoFunc();
        //userMotionNightBindFunc();
        //userMotionRelayBindFunc();
        if(userMotionDetectFlag)
        {
           if(userMotionDetect==0)
            {
                userMotionDetect = 1;
                userSendMotionInfoFunc();
            }
        }
          
    }
     userMotionRelayNightBindFunc();
    _NOP;
   
}

unsigned int userRelayCloseDelayArray[] = {5,10,20,30,60,120,300,600,900,1800,3600,7200,18000};
unsigned char userRelayOperateState = 0;
unsigned char userNightOperateState = 0;
unsigned char userAutoCloseFlag = 0;
void userMotionRelayNightBindFunc(void)
{
  
    if(userMotionEnable==0)return ;
    if(userMotionDetect)
    {
        //if(userAutoCloseFlag && userRelayCloseTime) 
        if(userAutoCloseFlag)
        {
            userRelayCloseTime = 0;
            userMotionCntA = 0;
            return ;
        }
        if(userLightSensorDetectLevel <= userLightSensorLevelDefault)
     //   if(userLightSensorDetectLevel < 10)
        {
            userAutoCloseFlag = 1;
            //userMotionBackLightBindFunc();
            if(userRelayOperateState==0x00) 
            {
                if(userRelayDefaultBindEnable)
                {
                    userRelayCtrlFunc(3,0x29);
                    userRelayOperateState=0x02;
                    userRelayCloseTime = 0;
                    userMotionCntA = 0;
                }
            }
            if(userNightOperateState==0x00)
            {
                if(userLightDefaultBindEnable)
                {
                    userNightOn();
                    userNightOperateState=0x02;
                    userRelayCloseTime = 0;
                    userMotionCntA = 0;
                    userSendNightInfoFunc();
                }
            }
        }
    }
    else
    {
        if(userRelayCloseTime >= userRelayCloseDelayArray[userCloseDelay-1])
        {
            userAutoCloseFlag = 0;
            userRelayCloseTime = 0;
            //userMotionBackLightBindFunc();
            if(userRelayOperateState==0x02)
            {
                if(userRelayDefaultBindEnable)
                {
                    userRelayCtrlFunc(3,0x28);
                    userRelayOperateState=0x00;
                }
            }
            if(userNightOperateState==0x02)
            {
               if(userLightDefaultBindEnable)
                {
                    userNightOff();
                    userNightOperateState=0x00;
                    userSendNightInfoFunc();
                }
            }
        }
    }
 
}




void  userMotionRelayBindFunc(void)
{
    
    if(userMotionEnable==0)return ;
    if(userRelayDefaultBindEnable)
    {
        if(userMotionDetect)  
        {
            userRelayCtrlFunc(3,0x29); 
            userRelayOperateState=0x02;
        }  
        else  
        {
            userRelayCtrlFunc(3,0x28);
            userRelayOperateState=0x00;
        }
    }
   
}

void userMotionNightBindFunc(void)
{
     
    if(userMotionEnable==0)return ;
    if(userLightDefaultBindEnable)
    {
        if(userMotionDetect)  
        {   
            userNightOn();
            userNightOperateState=0x02;
        }
        else   
        {
            userNightOff();
            userNightOperateState=0x00;
        }
        userSendNightInfoFunc();
    }
    else
    {
        userNightOff();
        userNightOperateState=0x00;
        userSendNightInfoFunc();
    }
   
}

void userMotionBackLightBindFunc(void)
{
    //if(userLightSensorDetectLevel == userLightSensorDetectLevelPre) return ;
     if(userLightSensorEnable)
     {
        if(userMotionDetect)    userBackLedSetFunc(userLightSensorDetectLevel);
         else   userBackLedSetFunc(0x00);
     }
     else
     {
        if(userMotionDetect)    userBackLedSetFunc(0xFF);
         else   userBackLedSetFunc(0x00);
     }
}
void userMotionCtrlFunc(unsigned char ctrlValue)
{
    if(ctrlValue)
    {
        userMotionLevel = 1;
    }
    else
    {
        userMotionLevel = 0;
    }
}

uint8 uZeroLevel = 0;
void userInputZeroDetectFunc(void)
{
   
    static unsigned int uZeroHCnt = 0;
    static unsigned int uZeroLCnt = 0;
    if(userStartFlag) return ;
    if(InputZero_Read())
    {
        uZeroLCnt=0;
        if((uZeroLevel & 0x01)==0)  uZeroLevel |= 0x10;
        uZeroLevel |= 0x01;
        if(uZeroHCnt < 2000)
        {
            uZeroHCnt++;
            return ;
        }
        if(userZeroDetect==0)  
        {      
            userZeroDetect=1;
            userSendZeroDetectInfoFunc();
        }
        uZeroHCnt = 0;
        
    }
    else
    {
        uZeroHCnt = 0;
        if(uZeroLevel & 0x01) uZeroLevel |= 0x10;
        uZeroLevel &= 0xFE;
        if(uZeroLCnt < 2000)
        {
            uZeroLCnt++;
            return ;
        }
        if(userZeroDetect==0)  
        {      
            userZeroDetect=1;
            userSendZeroDetectInfoFunc();
        }
        uZeroLCnt = 0;
    }  
  
}

void userSensorConfigSetFunc(unsigned char uCmdA,unsigned char uCmdB)
{
  
    if(uCmdA&0x01) userLightDefaultBindEnable = 1;
    else   userLightDefaultBindEnable = 0;
    
    if(uCmdA&0x02)  userRelayDefaultBindEnable = 1;
    else   userRelayDefaultBindEnable = 0;

    userCloseDelay = uCmdB & 0x0F;
    if(userCloseDelay > 13) userCloseDelay = 13;
    if(userCloseDelay == 0) userCloseDelay = 1;
    userLightSensorLevelDefault = uCmdB & 0xF0;
    userLightSensorLevelDefault >>= 4;
    if(userLightSensorLevelDefault > 10)    userLightSensorLevelDefault = 10;
    if(userLightSensorLevelDefault == 0)    userLightSensorLevelDefault = 1;
    

}

